require(["config"], function(config) {
	require(["jquery", "sw", "cook", "cooki", "one"], function(jquery, sw, cook, cooki, one) {
		/*var mySwiper = new Swiper(".swiper-container",{
			loop:true,
			autoplay:3000,
			pagination: '.swiper-pagination',
			paginationClickable :true,
			effect : 'fade'
		});	*/

		$(function() {
			//得焦
			$(".main_right_bottom input").focus(function() {
					$(this).css("border", "1px solid red");
					if ($(this).attr("name") == "id") {
						//$(this).siblings().css({"display":"block","color":"red"}).html("请输入手机号")
					} else {
						//$(this).siblings().css("display","none")
					}
				})
				//失焦
			$(".main_right_bottom input").blur(function() {
				$(this).css("border", "1px solid #dedede")

				if ($(this).attr("name") == "id") {
					var reg = /^1[3|4|5|7|8][0-9]\d{4,8}$/i;
					if ($(this).val() == "" || $(this).val() == "请输入手机号") {
						$(this).siblings().css({
							"display": "block",
							"color": "red"
						}).html("手机号码不能为空");
						$(this).siblings().css("display", "block");
					} else if ($(this).val().length < 11) {
						$(this).siblings().css({
							"display": "block",
							"color": "red"
						}).html("请输入正确的手机号码！");
						$(this).siblings().css("display", "block");
					} else if (!reg.test($(this).val())) {
						$(this).siblings().css({
							"display": "block",
							"color": "red"
						}).html("手机号不存在!");
						$(this).siblings().css("display", "block");
					} else {
						$(this).siblings().css("display", "none");;
						//console.log("hah")
					}

					//获取cookie
					var user = $(this).val()
					var users = getCookie("test") ? getCookie("test") : "";
					users = convertStrToObj(users);
					if (user in users) {
						alert("用户名已经存在");
						return;
					}

				}
				//判断密码为空
				if ($(this).attr("name") == "password") {
					reg = /^[\@A-Za-z0-9\!\-\_]{6,20}$/;

					if ($(this).val() == "请输入密码") {
						$(this).siblings().css({
							"display": "block",
							"color": "red"
						}).html("请输入密码");
						$(this).siblings().css("display", "block");
					} else if (!reg.test($(this).val())) {
						$(this).siblings().css({
							"display": "block",
							"color": "red"
						}).html("6-20位大小写字母，数字及'-'、'_'组合");
						$(this).siblings().css("display", "block");
					} else {
						$(this).siblings().css("display", "none");
					}
				}
				//再次输入密码
				if ($(this).attr("equalTo") == "#password" && $(this).val() == "") {
					$(this).css("border", "1px solid #ff0000");
					$(this).prev().removeClass("remind");
					$(this).siblings().css({
						"display": "block",
						"color": "red"
					}).html("请再次输入密码");
				}

			})
			$("#repassword").blur(function() {
				if ($("#repassword").val() != $("#password").val()) {
					$(this).css("border", "1px solid #ff0000");
					$(this).siblings().css({
						"display": "block",
						"color": "red"
					}).html("密码不一致")
				} else {
					$(this).siblings().css("display", "none");
				}

			})
			$('.submit').click(function() {

				if ($('input[name="id"]').val() != '' && $('input[name="id"]').val().length == 11 && $('input[name="password"]').val() != '' && $('input[id="repassword"]').val() != '' &&
					$('input[id="repassword"]').val() == $('input[name="password"]').val()) {

					var user = $('input[name="id"]').val();
					var pwd = $('input[name="password"]').val()
					var pwd1 = $('input[id="repassword"]').val()

					var users = getCookie("test") ? getCookie("test") : ""; //统一数据类型
					users = convertStrToObj(users);
					if (pwd != pwd1) {
						alert("两次输入密码不一致");
						return
					}
					//获取cookie
					if (user in users) {
						alert("用户名已存在");
						return
					} else {
						users[user] = pwd; //将密码和用户名进行配对
						var users = convertObjToStr(users); //将对象转换成字符串，才能创建cookie
						setCookie("test", users, 7); //创建cookie
						console.log(decodeURIComponent(document.cookie));
						alert("注册成功");

					}
				}
			})

			function convertStrToObj(str) {
				if (!str) {
					return {}
				}
				var users = str.split(":")
				var obj = {}
				for (var i = 0; i < users.length; i++) {
					var userData = users[i].split(",")
					obj[userData[0]] = userData[1]
				}
				return obj
			}

			function convertObjToStr(obj) {
				//假设不为空："test1,abc:test2,123"
				var str = "";
				for (var usn in obj) {
					var pwd = obj[usn];
					if (str) {
						//看是否是第一组用户名和密码，如果不是，先在前面添加一个：
						str += ":";
					}
					str += usn + ',' + pwd;
					console.log(str)
				}
				return str;
			}

		})

	});

});